#!/bin/bash

folder=$1   #fuzzer result folder
pno=$2      #port number
fmode=$3    #file mode -- structured or not
            #fmode = 0: the test case is a concatenated message sequence -- there is no message boundary
            #fmode = 1: the test case is a structured file keeping several request messages

#files stored in replayable-* folders are structured
#in such a way that messages are separated
testdir=
replayer=
if [ $fmode -eq "1" ]; then
  testdir="replayable-crashes"
  replayer="aflnet-replay"
else
  testdir="crashes"
  replayer="afl-replay"
fi

#process fuzzer-generated testcases
count=0
for f in $(echo $folder/$testdir/id* | grep -v "*"); do 

  echo $f

  (sleep 1 && $replayer $f HTTP $pno 100 10000 > /dev/null 2>&1) &
  timeout -k 0 -s SIGUSR1 10s ./forked-daapd/src/forked-daapd -d 0 -c ./forked-daapd.conf -f > /dev/null 2>&1
 

  wait

  if [ $? -ge 128 ]; then
    echo "Terminated: $?"
  fi

  echo
  echo

done

